import listCard from '../../../components/acessory/listCard'
import { dateFormat, dayDiff } from '@/utils/formatDate.js'

export default {
  components: {
    listCard,
  },

  data() {
    return {
      info: null,
      id: '',
      remainder: '', //报价有效期剩余时间
      timestamp: 10000, //倒计时时间
      popup: {
        show: false,
        title: '',
        btnText: '',
        mode: 'middle',
        val: '',
        pageFrom: '', //从哪里进入详情
      },
    }
  },
  computed: {
    statusIcon() {
      return this.$store.state.accessory.inquiryOrderStatusIcon
    },
    status() {
      return this.$store.state.accessory.inquiryOrderStatus
    },
    quality() {
      return this.$store.state.accessory.quality
    },
    storeId() {
      return this.$store.state.userInfoDetail.store.storeId
    },
  },

  onLoad(e) {
    this.id = e.id
    this.pageFrom = e.pageFrom ? e.pageFrom : ''
  },

  onShow() {
    this.getInfo()
  },

  methods: {
    dateFormat,
    dayDiff,

    getInfo() {
      uni.showLoading({
        title: '加载中',
      })
      this.$api.accessory
        .orderDetail({
          order_parts_id: this.id,
        })
        .then((res) => {
          console.log(res)
          this.info = res.data
          this.getTimestamp(this.info.closing_data)
          if (this.info.status === 0) {
            this.remainder = this.dayDiff(this.info.effective_time)
          }
          uni.hideLoading()
        })
        .catch((e) => {
          console.log(e)
          uni.hideLoading()
        })
    },

    // 订单操作弹窗 取消报价 确认物流信息 删除
    handleOrder(val) {
      if (val === 'del') {
        this.popup.show = true
        this.popup.title = '请确认您是否删除此订单'
        this.popup.btnText = '确认删除'
        this.popup.val = val
      } else if (val === 'getGoods') {
        this.popup.show = true
        this.popup.title = '请确认您是否已收到完好货品？'
        this.popup.btnText = '确认收货'
        this.popup.val = val
      } else if (val === 'cancel') {
        this.popup.show = true
        this.popup.title = '请确认您是否取消此订单'
        this.popup.btnText = '确认取消'
        this.popup.val = val
      }
    },

    popupFn() {
      if (this.popup.val === 'del') {
        this.deleteFn()
      } else if (this.popup.val === 'getGoods') {
        this.confirmReceive()
      } else if (this.popup.val === 'cancel') {
        this.cancelFn()
      }
    },

    //去付款
    gotoPay() {
      uni.navigateTo({
        url:
          '/pages_order/pay/pay?orderNo=' +
          this.info.id +
          '&price=' +
          this.info.total_money +
          '&type=accessory',
      })
    },

    //取消订单
    cancelFn() {
      this.$api.accessory
        .cancelOrder({
          id: this.id,
          store_id: this.storeId,
        })
        .then((res) => {
          this.popup.show = false
          if (res.data) {
            uni.showToast({
              title: '订单已取消',
              duration: 2000,
              icon: 'success',
            })
          } else {
            uni.showToast({
              title: '订单取消异常，请稍后重试',
              icon: 'none',
              duration: 2000,
            })
          }
          setTimeout(() => {
            this.getInfo()
          }, 2000)
        })
        .catch((e) => {
          console.log(e)
          this.popup.show = false
        })
    },

    //确认收货
    confirmReceive() {
      this.$api.accessory
        .confirmReceive({
          id: this.id,
          store_id: this.storeId,
        })
        .then((res) => {
          this.popup.show = false
          if (res.data) {
            uni.showToast({
              title: '收货成功',
              duration: 2000,
              icon: 'success',
            })
          } else {
            uni.showToast({
              title: '收货异常，请稍后重试',
              icon: 'none',
              duration: 2000,
            })
          }
          setTimeout(() => {
            this.getInfo()
          }, 2000)
        })
        .catch((e) => {
          console.log(e)
          this.popup.show = false
        })
    },

    //删除
    deleteFn() {
      this.$api.accessory
        .deleteOrder({
          id: this.id,
          store_id: this.storeId,
        })
        .then((res) => {
          this.popup.show = false
          if (res.data) {
            uni.showToast({
              title: '订单已删除',
              duration: 2000,
              icon: 'success',
            })
          } else {
            uni.showToast({
              title: '订单删除异常，请稍后重试',
              icon: 'none',
              duration: 2000,
            })
          }
          setTimeout(() => {
            uni.navigateBack()
          }, 2000)
        })
        .catch((e) => {
          console.log(e)
          this.popup.show = false
        })
    },

    //打电话
    call() {
      uni.makePhoneCall({
        phoneNumber: this.info.sell_phone,
      })
    },

    //联系商家
    chat() {
      if (this.pageFrom === 'chat') {
        uni.navigateBack()
        return
      }
      let carPartDetailsStr = ''
      for (let i = 0; i < this.info.snapshootlist.length; i++) {
        if (i > 4) {
          break
        }
        carPartDetailsStr =
          carPartDetailsStr + this.info.snapshootlist[i].parts_name + ' '
      }
      this.$store.commit('news/createConversationActive', {
        user: this.info.store_sell_name, //店铺名称
        img: this.info.store_sell_topimg, //店铺头像
        lastTime: '', //
        userId: this.info.store_sell_id, //店铺id
        conversationID: 'C2C' + this.info.store_sell_id,
      })
      this.$store.commit('news/setCardInfo', {
        imgSrc: 'https://files.yzsheng.com' + this.info.brand_img,
        name: this.info.car_name,
        carPartDetailsStr: carPartDetailsStr,
        type: 'accessoryOrder',
      })
      uni.navigateTo({
        url:
          '/pages_news/contactSeller/contactSeller?user=' +
          this.info.store_sell_name +
          '&pageId=' +
          this.info.id +
          '&tab=0',
      })
    },

    //复制
    copy(id) {
      // #ifndef H5
      uni.setClipboardData({
        data: id,
        success: function (res) {
          console.log(res)
          uni.showToast({
            title: '复制成功',
            icon: 'none',
          })
        },
      })
      // #endif
    },

    //图片处理
    handleImg(imgStr) {
      return JSON.parse(imgStr)
    },

    //倒计时转化为秒
    getTimestamp(str) {
      const arr = str.split(':')
      this.timestamp =
        Number(arr[0]) * 3600 + Number(arr[1]) * 60 + Number(arr[2])
    },

    //跳转店铺
    gotoStore() {
      uni.navigateTo({
        url: '/pages_common/store/store?id=' + this.info.store_sell_id,
      })
    },
  },
}
